# Copyright (c) 2005,2006,2007 Dynamic Solutions Inc. (support@dynamic-solutions.com)
#
# This file is part of DataZoomer.
#
# DataZoomer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DataZoomer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""The system's debugger object"""


import StringIO, time, os, inspect

class DzDebugger:

    def __init__(self,prefix):
        self.textcolor  = 'black'
        self.color      = 'red'
        self.prefix     = prefix
        self.banner_printed = 0
        self.buffer     = StringIO.StringIO()
        self.line       = 0
        self.start_time = time.time()

    def get_banner(self):
        if self.banner_printed:
            return 0
        out = """
            <br><br><font size=2 color="%s">
            <strong>Debugger started for %s</strong>
            </font><br><hr>
        """ % (self.textcolor,self.prefix)
        return out

    def print_banner(self):
        print self.get_banner()

    def __call__(self,msg):
        frame = inspect.stack()[1]
        if frame[3] == 'debug':
            inspect.stack()[2]
        (p,n) = os.path.split(frame[1])
        t = '%s,%s,%s: %s' % (n,frame[3],frame[2],msg)
        del frame
        return self.write(t)

    def write(self,msg):
        msg = """
            <font size=1 color=%s>%03d &nbsp;</font>
            <font size=1 color=%s>%s</font><br>
        """ % (self.textcolor,self.line,self.color,msg)
        self.buffer.write(msg)
        self.line += 1

    def flush_buffer(self):
        self.print_banner()
        print self.buffer.getvalue()
        print '<font size=1>Run time: %2.4fs</font><br><br>' % (time.time() - self.start_time)
        self.buffer.truncate(0)

    def content(self):
        banner  = self.get_banner()
        result  = self.buffer.getvalue()
        runtime = '<font size=1>Run time: %2.4fs</font><br><br>' % (time.time() - self.start_time)
        self.buffer.truncate(0)
        return '<div id="Debug">%s%s%s</div>' % (banner,result,runtime)


if __name__ == '__main__':
    import unittest
    class LogTest(unittest.TestCase):
        def test(self):
          debug = DzDebugger('unittest')
          debug.write('hello')
          debug.color='blue'
          debug.textcolor='red'
          debug.write('test 2')
          self.assert_(debug.buffer.__dict__['buflist'][0].find('<font size=1 color=black>000')!=-1)
          self.assert_(debug.buffer.__dict__['buflist'][0].find('<font size=1 color=red>hello')!=-1)
          self.assert_(debug.buffer.__dict__['buflist'][1].find('<font size=1 color=red>001')!=-1)
          self.assert_(debug.buffer.__dict__['buflist'][1].find('<font size=1 color=blue>test 2')!=-1)
          content = debug.content()
          self.assert_(content.find('<div id="Debug">')!=-1)
          self.assert_(content.find('Debugger started for unittest')!=-1)
          self.assert_(content.find('<font size=1 color=black>000')!=-1)
          self.assert_(content.find('<font size=1 color=red>hello')!=-1)
          self.assert_(content.find('<font size=1 color=red>001')!=-1)
          self.assert_(content.find('<font size=1 color=blue>test 2')!=-1)
          debug.line -= 1
          debug.write('more stuff')
          debug.flush_buffer()
    unittest.main()
